import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login/login';
import errorPage from '@/components/errorpage';
import {indexRoutes} from '@/components/index/router/index.routes';
import {orderRoutes} from '@/components/order/router/order.routes';
import {userRoutes} from '@/components/user/router/user.routes';
import Axios from 'axios'

Vue.use(Router)

let userinfo = localStorage.userinfo ? JSON.parse(localStorage.userinfo) : null;

let router = new Router({
  // mode: 'history',
  routes: [
    //路径重定向
    { path: '/', redirect: '/login' },
    ...indexRoutes,
    ...orderRoutes,
    ...userRoutes,
    {
      path: '/login',
      name: '登录',
      component: login
    },
    //默认错误地址跳转页
    {
      path: '*',
      name: 'errorPage',
      component: errorPage
    }
  ]
});
export default router;

//路由权限跳转控制
router.beforeEach((to, from, next) => {
  let path = to.matched.filter(v => v.meta.requireAuth);

  //抓取访问记录
  if (userinfo == null) {
    userinfo = {
      Name: '未登录',
      LogonUser: '',
    }
  }
  let log = `登录用户：${userinfo.Name}；账号：${userinfo.LogonUser}；访问页面：${to.name}，路径：${location.protocol}//${location.host}${to.path}`,
    JLog = {
      UserName: userinfo.Name,
      LogonUser: userinfo.LogonUser,
      PageName: to.name,
      Path: `${location.protocol}//${location.host}${to.path}`,
      Content: log,
    };
  Axios.post(`${process.env.API_ROOT}/api/webchart/AccessRecording`, JLog);
  //抓取访问记录


  if (path.length) {
    // let metaName = path[0].meta.requireAuth,
    //     adminLogin = (metaName=="admin" && !cookie.getCookie('accountId')),
    //     manaLogin = (metaName=="manager" && !cookie.getCookie('managerId'));
    // if(adminLogin || manaLogin){
    //   next({
    //     path: '/login',
    //   })
    // }else{
    //   next();
    // }
  } else {
    next();
  }
})